package com.botao.cms.log;

import com.botao.cms.dao.LogMapper;
import com.botao.cms.pojo.Log;
import com.botao.cms.pojo.User;
import com.botao.cms.utils.RequestUtils;
import com.botao.cms.utils.UUIDUtil;
import com.botao.cms.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Slf4j
@Aspect
@Component
public class UserLog {
    @Autowired
    LogMapper logMapper;

    @Before(value = "execution(* com.botao.cms.controller.BaseController.loginCheck(..))")
    public void loginLog(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        HttpServletRequest request = RequestUtils.getRequest();
        String host = request.getRemoteHost();
        log.info("{}正在登陆···", args[0].toString());
        log.info("主机IP地址：{}", host);
        Log log = new Log()
                .setId(UUIDUtil.getuuid())
                .setUser(args[0].toString())
                .setHost(request.getRemoteHost())
                .setAction("登陆")
                .setCreated(new Date())
                .setLevel(0);
        logMapper.insert(log);
    }


    @AfterReturning(value = "execution(* com.botao.cms.controller.UserController.deletedUserById(..))")
    public void deleteUserLog(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();

        HttpServletRequest request = RequestUtils.getRequest();
        User user = UserUtils.getSessionUser();
        log.info("删除{}", args[0]);
        Log log = new Log()
                .setId(UUIDUtil.getuuid())
                .setUser(user.getEmail())
                .setHost(request.getRemoteHost())
                .setAction("删除用户" + args[0].toString())
                .setCreated(new Date())
                .setLevel(2);
        logMapper.insert(log);
    }

    @AfterReturning(value = "execution(* com.botao.cms.service.UserService.addUser(..))")
    public void addLog(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();

        HttpServletRequest request = RequestUtils.getRequest();
        log.info("添加用户" + Arrays.toString(args));
        Log log = new Log()
                .setId(UUIDUtil.getuuid())
                .setUser(UserUtils.getSessionUser().getEmail())
                .setHost(request.getRemoteHost())
                .setAction("添加用户")
                .setCreated(new Date())
                .setLevel(0);
        logMapper.insert(log);
    }

    @AfterReturning(value = "execution(* com.botao.cms.service.UserService.sendEmailToUser(..))")
    public void sendEmailToUser(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        log.info("发送私信=>" + Arrays.toString(args));
    }

    @AfterReturning(value = "execution(* com.botao.cms.poi.ExportUserInFo.export(..))")
    public void exportUserInFo(JoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = RequestUtils.getRequest();
        User user = UserUtils.getSessionUser();
        log.warn("{}正在导出用户信息···", user.getEmail());
        Log log = new Log()
                .setId(UUIDUtil.getuuid())
                .setUser(user.getEmail())
                .setHost(request.getRemoteHost())
                .setAction("导出用户信息")
                .setCreated(new Date())
                .setLevel(2);
        logMapper.insert(log);
    }
}
